<style scoped lang="scss">
@import url('index.scss');
</style>

<template>
  <div class="app-container">

    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="药品编码" prop="drugCode">
              <el-input v-model="queryParams.drugCode" placeholder="请输入药品编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="药品通用名称" prop="drugName">
              <el-input v-model="queryParams.drugName" placeholder="请输入药品通用名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="核素" prop="drugIsotope">
              <el-input v-model="queryParams.drugIsotope" placeholder="请输入核素" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="半衰期" prop="halfLife">
              <el-input v-model="queryParams.halfLife" placeholder="请输入半衰期" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="生产厂家" prop="producer">
              <el-input v-model="queryParams.producer" placeholder="请输入生产厂家" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="批准文号" prop="approvalno">
              <el-input v-model="queryParams.approvalno" placeholder="请输入批准文号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="每支/瓶标称活度(MBq)" prop="unitVolMbq">
        <el-input
          v-model="queryParams.unitVolMbq"
          placeholder="请输入每支/瓶标称活度(MBq)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建议成人用量(MBq)" prop="unitDoseMbq">
        <el-input
          v-model="queryParams.unitDoseMbq"
          placeholder="请输入建议成人用量(MBq)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="有效期" prop="shelfLife">
              <el-input v-model="queryParams.shelfLife" placeholder="请输入有效期" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="存放位置" prop="drugArea">
              <el-input v-model="queryParams.drugArea" placeholder="请输入存放位置" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button class="newButton" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:drugdict:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:drugdict:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:drugdict:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:drugdict:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
      </el-row>
    </div>



    <!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:drugdict:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:drugdict:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:drugdict:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:drugdict:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="drugdictList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" prop="drugid" />
      <el-table-column label="药品编码" align="center" prop="drugCode" />
      <el-table-column label="药品通用名称" align="center" prop="drugName" />
      <el-table-column label="药品价格" align="center" prop="drugPrice" />
      <el-table-column label="核素" align="center" prop="drugIsotope" />
      <el-table-column label="半衰期" align="center" prop="halfLife" />
      <el-table-column label="生产厂家" align="center" prop="producer" />
      <el-table-column label="批准文号" align="center" prop="approvalno" />
      <el-table-column label="每支/瓶标称活度(MBq)" align="center" prop="unitVolMbq" />
      <el-table-column label="建议成人用量(MBq)" align="center" prop="unitDoseMbq" />
      <el-table-column label="有效期" align="center" prop="shelfLife" />
      <el-table-column label="药品类别" align="center" prop="drugType" />
      <el-table-column label="存放位置" align="center" prop="drugArea" />
      <el-table-column label="药物状态" align="center" prop="status" >
        <template #default="scope">
          <el-tag :type="scope.row.status === '0' ? 'danger' : 'success'">{{ scope.row.status === '0' ? '停用' : '正常' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:drugdict:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:drugdict:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改药品列表对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="drugdictRef" :model="form" :rules="rules" label-width="160px">
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="药品通用名称" prop="drugName">
          <el-input v-model="form.drugName" placeholder="请输入药品通用名称" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="药品编码" prop="drugCode">
          <el-input v-model="form.drugCode" placeholder="请输入药品编码" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="生产厂家" prop="producer">
          <el-input v-model="form.producer" placeholder="请输入生产厂家" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="批准文号" prop="approvalno">
          <el-input v-model="form.approvalno" placeholder="请输入批准文号" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="药品类别" prop="drugType">
          <el-input v-model="form.drugType" placeholder="请输入药品类别" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="药品价格" prop="drugPrice">
          <el-input v-model="form.drugPrice" placeholder="请输入药品价格" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="核素" prop="drugIsotope">
          <el-input v-model="form.drugIsotope" placeholder="请输入核素" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="半衰期" prop="halfLife">
          <el-input v-model="form.halfLife" placeholder="请输入半衰期" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="每支/瓶标称活度(MBq)" prop="unitVolMbq">
          <el-input v-model="form.unitVolMbq" placeholder="请输入每支/瓶标称活度(MBq)" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="建议成人用量(MBq)" prop="unitDoseMbq">
          <el-input v-model="form.unitDoseMbq" placeholder="请输入建议成人用量(MBq)" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="有效期" prop="shelfLife">
          <el-input v-model="form.shelfLife" placeholder="请输入有效期" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="存放位置" prop="drugArea">
          <el-input v-model="form.drugArea" placeholder="请输入存放位置" />
        </el-form-item>
          </el-col>
        </el-row>        
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="存储方法" prop="storageMethod">
          <el-input v-model="form.storageMethod" placeholder="请输入存储方法" />
        </el-form-item>
          </el-col>
        </el-row>        
        <el-row :gutter="20">
         <el-col :span="24">
          <el-form-item label="药物状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in ha_normal_status" :key="dict.value" :value="dict.value" :label="dict.value">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
          </el-col>
        </el-row>                         
        <el-row :gutter="20">
          <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="4" />
        </el-form-item>
          </el-col>
        </el-row>     

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Drugdict">
import { listDrugdict, getDrugdict, delDrugdict, addDrugdict, updateDrugdict } from "@/api/system/drugdict"

const { proxy } = getCurrentInstance()
const { ha_normal_status } = proxy.useDict('ha_normal_status'); // 使用字典

const drugdictList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    drugCode: null,
    drugName: null,
    drugPrice: null,
    drugIsotope: null,
    halfLife: null,
    producer: null,
    approvalno: null,
    unitVolMbq: null,
    unitDoseMbq: null,
    shelfLife: null,
    drugType: null,
    drugArea: null,
    storageMethod: null,
    status: null,
  },
  rules: {
    drugCode: [{ required: true, message: '请输入药品编码', trigger: 'blur' }],
    drugName: [{ required: true, message: '请输入药品名称', trigger: 'blur' }],
    drugPrice: [{ required: true, message: '请输入药品价格', trigger: 'blur' }],
    producer: [{ required: true, message: '请输入生产商', trigger: 'blur' }],
    approvalno: [{ required: true, message: '请输入批准文号', trigger: 'blur' }],
    storageMethod: [{ required: true, message: '请输入存储方法', trigger: 'blur' }],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询药品列表列表 */
function getList() {
  loading.value = true
  listDrugdict(queryParams.value).then(response => {
    drugdictList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    drugid: null,
    drugCode: null,
    drugName: null,
    drugPrice: null,
    drugIsotope: null,
    halfLife: null,
    producer: null,
    approvalno: null,
    unitVolMbq: null,
    unitDoseMbq: null,
    shelfLife: null,
    drugType: null,
    drugArea: null,
    storageMethod: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    status: '1',
    remark: null
  }
  proxy.resetForm("drugdictRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.drugid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加药品列表"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _drugid = row.drugid || ids.value
  getDrugdict(_drugid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改药品列表"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["drugdictRef"].validate(valid => {
    if (valid) {
      if (form.value.drugid != null) {
        updateDrugdict(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDrugdict(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _drugids = row.drugid || ids.value
  proxy.$modal.confirm('是否确认删除药品列表编号为"' + _drugids + '"的数据项？').then(function() {
    return delDrugdict(_drugids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/drugdict/export', {
    ...queryParams.value
  }, `drugdict_${new Date().getTime()}.xlsx`)
}

getList()
</script>
